<template>
	<xn-form-container
		title="删除线索"
		:width="600"
		:visible="visible"
		:destroy-on-close="true"
		:body-style="{ 'padding-top': '0px' }"
		@close="onClose"
	>
		<div class="flex items-center gap-2 bg-gray-200 mt-5 p-2">
			<ExclamationCircleFilled style="font-size: 18px; color: #6e6e6e" />
			<div>删除后将清除线索的所有资料信息，操作成功后将无法恢复</div>
		</div>
		<a-form ref="formRef" :model="formData" :rules="formRules" :label-col="{ style: { width: '80px' } }" class="p-10">
			<a-form-item label="删除原因" name="reasonId">
				<a-select
					v-model:value="formData.reasonId"
					:options="reasonOptions"
					:fieldNames="{
						label: 'name',
						value: 'id'
					}"
					placeholder="请选择删除原因"
				/>
			</a-form-item>
			<a-form-item label="备注" name="remark">
				<a-textarea v-model:value="formData.remark" :rows="4" placeholder="请填写详细原因" />
			</a-form-item>
		</a-form>
		<template #footer>
			<div class="flex items-center justify-between">
				<div>已选{{ list.length }}个线索</div>
				<a-space>
					<a-button type="primary" :loading="submitLoading" @click="onSubmit">确定</a-button>
					<a-button style="margin-right: 8px" @click="onClose">取消</a-button>
				</a-space>
			</div>
		</template>
	</xn-form-container>
</template>

<script setup>
	import { required } from '@/utils/formRules'
	import highSeasClueApi from '@/api/biz/highSeasClueApi'
	import deleteReasonApi from '@/api/biz/crm/deleteReasonApi'

	const submitLoading = ref(false)

	// 默认是关闭状态
	const visible = ref(false)
	const formRef = ref()
	const emit = defineEmits({ successful: null })

	// 表单数据
	const formData = ref({})

	const list = ref([])
	const reasonOptions = ref([])

	// 打开抽屉
	const onOpen = (value) => {
		visible.value = true
		list.value = value
		deleteReasonApi.list().then((data) => {
			reasonOptions.value = data
		})
	}
	// 关闭抽屉
	const onClose = () => {
		visible.value = false
	}

	// 默认要校验的
	const formRules = {
		reasonId: [required('请选择删除原因')]
	}
	// 验证并提交数据
	const onSubmit = () => {
		formRef.value.validate().then(() => {
			submitLoading.value = true
			highSeasClueApi
				.delete({
					...formData.value,
					poolIdParamList: list.value
				})
				.then(() => {
					onClose()
					formRef.value.resetFields()
					emit('successful')
				})
				.finally(() => {
					submitLoading.value = false
				})
		})
	}

	// 调用这个函数将子组件的一些数据和方法暴露出去
	defineExpose({
		onOpen
	})
</script>

<style scoped type="less"></style>
